using JT808Protocol.Common;
using System;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.BodyParserStrategy.Location.LocationAddition
{
    /// <summary>
    /// 胎压报警事件信息
    /// </summary>
    public class TpmsInfo
    {
        /// <summary>
        /// 胎压报警位置
        /// 报警轮胎位置编号
        /// 从左前轮开始以Z字形从00依次编号，编号与是否安装TPMS无关）
        /// </summary>
        [JsonPropertyName("胎压报警位置")] public byte Location { get; set; }

        /// <summary>
        /// 报警事件类型
        /// 0表示无报警，1表示有报警
        /// bit0：胎压（定时上报）
        /// bit1：胎压过高报警
        /// bit2：胎压过低报警
        /// bit3：胎温过高报警
        /// bit4：传感器异常报警
        /// bit5：胎压不平衡报警
        /// bit6：慢漏气报警
        /// bit7：电池电量低报警
        /// bit8~bit15：自定义
        /// </summary>
        [JsonPropertyName("报警事件类型")] public ushort AlarmType { get; set; }

        /// <summary>
        /// 胎压
        /// 单位 Kpa
        /// </summary>
        [JsonPropertyName("胎压(Kpa)")] public ushort Pressure { get; set; }

        /// <summary>
        /// 胎温
        /// 单位 ℃
        /// </summary>
        [JsonPropertyName("胎温(℃)")] public ushort Temperature { get; set; }

        /// <summary>
        /// 电量
        /// 单位 %
        /// </summary>
        [JsonPropertyName("电量(%)")] public ushort Electricity { get; set; }

        public bool ReadEntity(ref ReadOnlySpan<byte> source, int startIndex)
        {
            Location = source[startIndex];

            AlarmType = ParseHelper.Uint16(ref source, startIndex + 1);

            Pressure = ParseHelper.Uint16(ref source, startIndex + 3);

            Temperature = ParseHelper.Uint16(ref source, startIndex + 5);

            Electricity = ParseHelper.Uint16(ref source, startIndex + 7);

            return true;
        }
    }
}